package service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import dao.IRxfDao;
import entities.TRxf;
import interfaces.IRxfService;
import util.ParamUtil;
import utils.StringUtil;
import views.VRoleFunction;

@Service
public class RxfService implements IRxfService {

	@Autowired
	private IRxfDao rxfDao;

	public TRxf persist(TRxf entityBean) throws Exception {
		if (StringUtil.isEmpty(entityBean)) {
			return null;
		}
		if (StringUtil.isEmpty(entityBean.getRolNo()) || StringUtil.isEmpty(entityBean.getFunNo())) {
			return null;
		}
		rxfDao.insert(entityBean);
		return entityBean;
	}

	public Integer remove(TRxf entityBean) throws Exception {
		if (StringUtil.isEmpty(entityBean)) {
			return 0;
		}
		if (StringUtil.isEmpty(entityBean.getRolNo()) && StringUtil.isEmpty(entityBean.getFunNo())) {
			return 0;
		}
		return rxfDao.delete(entityBean);
	}

	public Integer expunge(Map<String, Object> paramMap) throws Exception {
		return rxfDao.clear(paramMap);
	}

	public Integer record(Map<String, Object> paramMap) throws Exception {
		return rxfDao.count(paramMap);
	}

	public ResultSet<TRxf> search(QuerySet<Object> querySet) throws Exception {
		ResultSet<TRxf> resultSet = new ResultSet<>();
		if (StringUtil.isEmpty(querySet)) {
			return resultSet;
		}
		PageInfo pageInfo = querySet.getPageInfo();
		SortInfo sortInfo = querySet.getSortInfo();
		Map<String, Object> paramMap = querySet.getParamMap();
		if (StringUtil.isEmpty(pageInfo)) {
			pageInfo = new PageInfo();
		}
		if (!StringUtil.isEmpty(sortInfo)) {
			paramMap = ParamUtil.sortMap(paramMap, sortInfo);
		}
		PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), false);
		List<TRxf> resultList = rxfDao.query(paramMap);
		pageInfo.setPageRecord(rxfDao.count(querySet.getParamMap()));
		pageInfo.allocation();
		resultSet.setResultList(resultList);
		resultSet.setPageInfo(pageInfo);
		return resultSet;
	}

	public ResultSet<VRoleFunction> relate(QuerySet<Object> querySet) {
		ResultSet<VRoleFunction> resultSet = new ResultSet<>();
		if (StringUtil.isEmpty(querySet)) {
			return resultSet;
		}
		PageInfo pageInfo = querySet.getPageInfo();
		SortInfo sortInfo = querySet.getSortInfo();
		Map<String, Object> paramMap = querySet.getParamMap();
		if (StringUtil.isEmpty(pageInfo)) {
			pageInfo = new PageInfo();
		}
		if (!StringUtil.isEmpty(sortInfo)) {
			paramMap = ParamUtil.sortMap(paramMap, sortInfo);
		}
		PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), false);
		List<VRoleFunction> resultList = rxfDao.extend(paramMap);
		pageInfo.setPageRecord(rxfDao.gross(querySet.getParamMap()));
		pageInfo.allocation();
		resultSet.setResultList(resultList);
		resultSet.setPageInfo(pageInfo);
		return resultSet;
	}

}
